package com.wuage.interfaces.auth;

import com.wuage.application.IUacAccountService;
import com.wuage.application.IUacPermissionService;
import com.wuage.domain.model.UacAccount;
import com.wuage.domain.model.UacPermission;
import com.wuage.infrastructure.utils.BeanUtils;
import com.wuage.infrastructure.utils.JwtUtils;
import com.wuage.infrastructure.utils.Response;
import com.wuage.interfaces.BaseController;
import com.wuage.interfaces.auth.vo.LoginUserVO;
import com.wuage.interfaces.auth.vo.UacPermissionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Shynin
 * @version 1.0
 * @date 2020-09-03 00:37
 */
@Api(tags = "{权限}-相关接口")
@RestController
@RequestMapping("/auth")
public class AuthController extends BaseController {
    @Resource
    private IUacAccountService uacAccountService;

    @Resource
    private IUacPermissionService menuService;

    @ApiOperation("获取用户菜单")
    @GetMapping("/menus")
    public Response<List<UacPermissionVO>> getUsers() {
        List<UacPermission> menuByRole = menuService.getPermissionByRole(getLoginUser().getRole());
        return new Response<>(UacPermissionVO.assembleMenus2VOs(menuByRole));
    }

    @ApiOperation("获取用户菜单-无层级")
    @GetMapping("/menus/all")
    public Response<List<UacPermissionVO>> getUserMenus() {
        List<UacPermission> menuByRole = menuService.getPermissionByRoleWithoutChildren(getLoginUser().getRole());
        return Response.success(BeanUtils.copyCollections(menuByRole,UacPermissionVO.class));
    }

    @ApiOperation("获取当前登录用户信息")
    @GetMapping("/loginUser")
    public Response<LoginUserVO> getLoginUserInfo() {
        UacAccount uacAccount = uacAccountService.getById(JwtUtils.getLoginUser().getId());
        LoginUserVO loginUserVO = new LoginUserVO();
        BeanUtils.copyProperties(uacAccount, loginUserVO);
        return new Response<>(loginUserVO);
    }
}
